home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / public / jpeg / libCLRjpeg4 / src / include4 / jpegdata.h < prev    next >
C/C++ Source or Header  |  1994-08-01  |  39KB  |  931 lines

  1. /*
  2.  * jpegdata.h
  3.  *
  4.  * Copyright (C) 1991, 1992, Thomas G. Lane.
  5.  * This file is part of the Independent JPEG Group's software.
  6.  * For conditions of distribution and use, see the accompanying README file.
  7.  *
  8.  * This file defines shared data structures for the various JPEG modules.
  9.  */
  10.  
  11.  
  12. /*
  13.  * You might need to change some of the following declarations if you are
  14.  * using the JPEG software within a surrounding application program
  15.  * or porting it to an unusual system.
  16.  */
  17.  
  18.  
  19. /* If the source or destination of image data is not to be stdio streams,
  20.  * these types may need work.  You can replace them with some kind of
  21.  * pointer or indicator that is useful to you, or just ignore 'em.
  22.  * Note that the user interface and the various jrdxxx/jwrxxx modules
  23.  * will also need work for non-stdio input/output.
  24.  */
  25.  
  26. typedef FILE * JFILEREF;    /* source or dest of JPEG-compressed data */
  27.  
  28. typedef FILE * IFILEREF;    /* source or dest of non-JPEG image data */
  29.  
  30.  
  31. /* These defines are used in all function definitions and extern declarations.
  32.  * You could modify them if you need to change function linkage conventions,
  33.  * as is shown below for use with C++.  Another application would be to make
  34.  * all functions global for use with code profilers that require it.
  35.  * NOTE: the C++ test does the right thing if you are reading this include
  36.  * file in a C++ application to link to JPEG code that's been compiled with a
  37.  * regular C compiler.  I'm not sure it works if you try to compile the JPEG
  38.  * code with C++.
  39.  */
  40.  
  41. #define METHODDEF static    /* a function called through method pointers */
  42. #define LOCAL      static    /* a function used only in its module */
  43. #define GLOBAL            /* a function referenced thru EXTERNs */
  44. #ifdef __cplusplus
  45. #define EXTERN      extern "C"    /* a reference to a GLOBAL function */
  46. #else
  47. #define EXTERN      extern    /* a reference to a GLOBAL function */
  48. #endif
  49.  
  50.  
  51. /* Here is the pseudo-keyword for declaring pointers that must be "far"
  52.  * on 80x86 machines.  Most of the specialized coding for 80x86 is handled
  53.  * by just saying "FAR *" where such a pointer is needed.  In a few places
  54.  * explicit coding is needed; see uses of the NEED_FAR_POINTERS symbol.
  55.  */
  56.  
  57. #ifdef NEED_FAR_POINTERS
  58. #define FAR  far
  59. #else
  60. #define FAR
  61. #endif
  62.  
  63.  
  64.  
  65. /* The remaining declarations are not system-dependent, we hope. */
  66.  
  67.  
  68. /*
  69.  * NOTE: if you have an ancient, strict-K&R C compiler, it may choke on the
  70.  * similarly-named fields in Compress_info_struct and Decompress_info_struct.
  71.  * If this happens, you can get around it by rearranging the two structs so
  72.  * that the similarly-named fields appear first and in the same order in
  73.  * each struct.  Since such compilers are now pretty rare, we haven't done
  74.  * this in the portable code, preferring to maintain a logical ordering.
  75.  */
  76.  
  77.  
  78.  
  79. /* This macro is used to declare a "method", that is, a function pointer. */
  80. /* We want to supply prototype parameters if the compiler can cope. */
  81. /* Note that the arglist parameter must be parenthesized! */
  82.  
  83. #ifdef PROTO
  84. #define METHOD(type,methodname,arglist)  type (*methodname) arglist
  85. #else
  86. #define METHOD(type,methodname,arglist)  type (*methodname) ()
  87. #endif
  88.  
  89. /* Forward references to lists of method pointers */
  90. typedef struct External_methods_struct * external_methods_ptr;
  91. typedef struct Compress_methods_struct * compress_methods_ptr;
  92. typedef struct Decompress_methods_struct * decompress_methods_ptr;
  93.  
  94.  
  95. /* Data structures for images containing either samples or coefficients. */
  96. /* Note that the topmost (leftmost) index is always color component. */
  97. /* On 80x86 machines, the image arrays are too big for near pointers, */
  98. /* but the pointer arrays can fit in near memory. */
  99.  
  100. typedef JSAMPLE FAR *JSAMPROW;    /* ptr to one image row of pixel samples. */
  101. typedef JSAMPROW *JSAMPARRAY;    /* ptr to some rows (a 2-D sample array) */
  102. typedef JSAMPARRAY *JSAMPIMAGE;    /* a 3-D sample array: top index is color */
  103.  
  104.  
  105. #define DCTSIZE        8    /* The basic DCT block is 8x8 samples */
  106. #define DCTSIZE2    64    /* DCTSIZE squared; # of elements in a block */
  107.  
  108. typedef JCOEF JBLOCK[DCTSIZE2];    /* one block of coefficients */
  109. typedef JBLOCK FAR *JBLOCKROW;    /* pointer to one row of coefficient blocks */
  110. typedef JBLOCKROW *JBLOCKARRAY;        /* a 2-D array of coefficient blocks */
  111. typedef JBLOCKARRAY *JBLOCKIMAGE;    /* a 3-D array of coefficient blocks */
  112.  
  113. typedef JCOEF FAR *JCOEFPTR;    /* useful in a couple of places */
  114.  
  115.  
  116. /* The input and output data of the DCT transform subroutines are of
  117.  * the following type, which need not be the same as JCOEF.
  118.  * For example, on a machine with fast floating point, it might make sense
  119.  * to recode the DCT routines to use floating point; then DCTELEM would be
  120.  * 'float' or 'double'.
  121.  */
  122.  
  123. typedef JCOEF DCTELEM;
  124. typedef DCTELEM DCTBLOCK[DCTSIZE2];
  125.  
  126.  
  127. /* Types for JPEG compression parameters and working tables. */
  128.  
  129.  
  130. typedef enum {            /* defines known color spaces */
  131.     CS_UNKNOWN,        /* error/unspecified */
  132.     CS_GRAYSCALE,        /* monochrome (only 1 component) */
  133.     CS_RGB,            /* red/green/blue */
  134.     CS_YCbCr,        /* Y/Cb/Cr (also known as YUV) */
  135.     CS_YIQ,            /* Y/I/Q */
  136.     CS_CMYK            /* C/M/Y/K */
  137. } COLOR_SPACE;
  138.  
  139.  
  140. typedef struct {        /* Basic info about one component */
  141.   /* These values are fixed over the whole image */
  142.   /* For compression, they must be supplied by the user interface; */
  143.   /* for decompression, they are read from the SOF marker. */
  144.     short component_id;    /* identifier for this component (0..255) */
  145.     short component_index;    /* its index in SOF or cinfo->comp_info[] */
  146.     short h_samp_factor;    /* horizontal sampling factor (1..4) */
  147.     short v_samp_factor;    /* vertical sampling factor (1..4) */
  148.     short quant_tbl_no;    /* quantization table selector (0..3) */
  149.   /* These values may vary between scans */
  150.   /* For compression, they must be supplied by the user interface; */
  151.   /* for decompression, they are read from the SOS marker. */
  152.     short dc_tbl_no;    /* DC entropy table selector (0..3) */
  153.     short ac_tbl_no;    /* AC entropy table selector (0..3) */
  154.   /* These values are computed during compression or decompression startup */
  155.     long true_comp_width;    /* component's image width in samples */
  156.     long true_comp_height;    /* component's image height in samples */
  157.     /* the above are the logical dimensions of the downsampled image */
  158.   /* These values are computed before starting a scan of the component */
  159.     short MCU_width;    /* number of blocks per MCU, horizontally */
  160.     short MCU_height;    /* number of blocks per MCU, vertically */
  161.     short MCU_blocks;    /* MCU_width * MCU_height */
  162.     long downsampled_width;    /* image width in samples, after expansion */
  163.     long downsampled_height; /* image height in samples, after expansion */
  164.     /* the above are the true_comp_xxx values rounded up to multiples of */
  165.     /* the MCU dimensions; these are the working dimensions of the array */
  166.     /* as it is passed through the DCT or IDCT step.  NOTE: these values */
  167.     /* differ depending on whether the component is interleaved or not!! */
  168. } jpeg_component_info;
  169.  
  170.  
  171. /* DCT coefficient quantization tables.
  172.  * For 8-bit precision, 'INT16' should be good enough for quantization values;
  173.  * for more precision, we go for the full 16 bits.  'INT16' provides a useful
  174.  * speedup on many machines (multiplication & division of JCOEFs by
  175.  * quantization values is a significant chunk of the runtime).
  176.  * Note: the values in a QUANT_TBL are always given in zigzag order.
  177.  */
  178. #ifdef EIGHT_BIT_SAMPLES
  179. typedef INT16 QUANT_VAL;    /* element of a quantization table */
  180. #else
  181. typedef UINT16 QUANT_VAL;    /* element of a quantization table */
  182. #endif
  183. typedef QUANT_VAL QUANT_TBL[DCTSIZE2];    /* A quantization table */
  184. typedef QUANT_VAL * QUANT_TBL_PTR;    /* pointer to same */
  185.  
  186.  
  187. typedef struct {        /* A Huffman coding table */
  188.   /* These two fields directly represent the contents of a JPEG DHT marker */
  189.     UINT8 bits[17];        /* bits[k] = # of symbols with codes of */
  190.                 /* length k bits; bits[0] is unused */
  191.     UINT8 huffval[256];    /* The symbols, in order of incr code length */
  192.   /* This field is used only during compression.  It's initialized FALSE when
  193.    * the table is created, and set TRUE when it's been output to the file.
  194.    */
  195.     boolean sent_table;    /* TRUE when table has been output */
  196.   /* The remaining fields are computed from the above to allow more efficient
  197.    * coding and decoding.  These fields should be considered private to the
  198.    * Huffman compression & decompression modules.
  199.    */
  200.     /* encoding tables: */
  201.     UINT16 ehufco[256];    /* code for each symbol */
  202.     char ehufsi[256];    /* length of code for each symbol */
  203.     /* decoding tables: (element [0] of each array is unused) */
  204.     UINT16 mincode[17];    /* smallest code of length k */
  205.     INT32 maxcode[18];    /* largest code of length k (-1 if none) */
  206.     /* (maxcode[17] is a sentinel to ensure huff_DECODE terminates) */
  207.     short valptr[17];    /* huffval[] index of 1st symbol of length k */
  208. } HUFF_TBL;
  209.  
  210.  
  211. #define NUM_QUANT_TBLS      4    /* quantization tables are numbered 0..3 */
  212. #define NUM_HUFF_TBLS       4    /* Huffman tables are numbered 0..3 */
  213. #define NUM_ARITH_TBLS      16    /* arith-coding tables are numbered 0..15 */
  214. #define MAX_COMPS_IN_SCAN   4    /* JPEG limit on # of components in one scan */
  215. #define MAX_SAMP_FACTOR     4    /* JPEG limit on sampling factors */
  216. #define MAX_BLOCKS_IN_MCU   10    /* JPEG limit on # of blocks in an MCU */
  217.  
  218.  
  219. /* Working data for compression */
  220.  
  221. struct Compress_info_struct {
  222. /*
  223.  * All of these fields shall be established by the user interface before
  224.  * calling jpeg_compress, or by the input_init or c_ui_method_selection
  225.  * methods.
  226.  * Most parameters can be set to reasonable defaults by j_c_defaults.
  227.  * Note that the UI must supply the storage for the main methods struct,
  228.  * though it sets only a few of the methods there.
  229.  */
  230.     compress_methods_ptr methods; /* Points to list of methods to use */
  231.  
  232.     external_methods_ptr emethods; /* Points to list of methods to use */
  233.  
  234.     IFILEREF input_file;    /* tells input routines where to read image */
  235.     JFILEREF output_file;    /* tells output routines where to write JPEG */
  236.  
  237.     long image_width;    /* input image width */
  238.     long image_height;    /* input image height */
  239.     short input_components;    /* # of color components in input image */
  240.  
  241.     short data_precision;    /* bits of precision in image data */
  242.  
  243.     COLOR_SPACE in_color_space; /* colorspace of input file */
  244.     COLOR_SPACE jpeg_color_space; /* colorspace of JPEG file */
  245.  
  246.     double input_gamma;    /* image gamma of input file */
  247.  
  248.     boolean write_JFIF_header; /* should a JFIF marker be written? */
  249.     /* These three values are not used by the JPEG code, only copied */
  250.     /* into the JFIF APP0 marker.  density_unit can be 0 for unknown, */
  251.     /* 1 for dots/inch, or 2 for dots/cm.  Note that the pixel aspect */
  252.     /* ratio is defined by X_density/Y_density even when density_unit=0. */
  253.     UINT8 density_unit;    /* JFIF code for pixel size units */
  254.     UINT16 X_density;    /* Horizontal pixel density */
  255.     UINT16 Y_density;    /* Vertical pixel density */
  256.  
  257.     short num_components;    /* # of color components in JPEG image */
  258.     jpeg_component_info * comp_info;
  259.     /* comp_info[i] describes component that appears i'th in SOF */
  260.  
  261.     QUANT_TBL_PTR quant_tbl_ptrs[NUM_QUANT_TBLS];
  262.     /* ptrs to coefficient quantization tables, or NULL if not defined */
  263.  
  264.     HUFF_TBL * dc_huff_tbl_ptrs[NUM_HUFF_TBLS];
  265.     HUFF_TBL * ac_huff_tbl_ptrs[NUM_HUFF_TBLS];
  266.     /* ptrs to Huffman coding tables, or NULL if not defined */
  267.  
  268.     UINT8 arith_dc_L[NUM_ARITH_TBLS]; /* L values for DC arithmetic-coding tables */
  269.     UINT8 arith_dc_U[NUM_ARITH_TBLS]; /* U values for DC arithmetic-coding tables */
  270.     UINT8 arith_ac_K[NUM_ARITH_TBLS]; /* Kx values for AC arithmetic-coding tables */
  271.  
  272.     boolean arith_code;    /* TRUE=arithmetic coding, FALSE=Huffman */
  273.     boolean interleave;    /* TRUE=interleaved output, FALSE=not */
  274.     boolean optimize_coding; /* TRUE=optimize entropy encoding parms */
  275.     boolean CCIR601_sampling; /* TRUE=first samples are cosited */
  276.     int smoothing_factor;    /* 1..100, or 0 for no input smoothing */
  277.  
  278.     /* The restart interval can be specified in absolute MCUs by setting
  279.      * restart_interval, or in MCU rows by setting restart_in_rows
  280.      * (in which case the correct restart_interval will be figured
  281.      * for each scan).
  282.      */
  283.     UINT16 restart_interval;/* MCUs per restart interval, or 0 for no restart */
  284.     int restart_in_rows;    /* if > 0, MCU rows per restart interval */
  285.  
  286. /*
  287.  * These fields are computed during jpeg_compress startup
  288.  */
  289.     short max_h_samp_factor; /* largest h_samp_factor */
  290.     short max_v_samp_factor; /* largest v_samp_factor */
  291.  
  292. /*
  293.  * These fields may be useful for progress monitoring
  294.  */
  295.  
  296.     int total_passes;    /* number of passes expected */
  297.     int completed_passes;    /* number of passes completed so far */
  298.  
  299. /*
  300.  * These fields are valid during any one scan
  301.  */
  302.     short comps_in_scan;    /* # of JPEG components output this time */
  303.     jpeg_component_info * cur_comp_info[MAX_COMPS_IN_SCAN];
  304.     /* *cur_comp_info[i] describes component that appears i'th in SOS */
  305.  
  306.     long MCUs_per_row;    /* # of MCUs across the image */
  307.     long MCU_rows_in_scan;    /* # of MCU rows in the image */
  308.  
  309.     short blocks_in_MCU;    /* # of DCT blocks per MCU */
  310.     short MCU_membership[MAX_BLOCKS_IN_MCU];
  311.     /* MCU_membership[i] is index in cur_comp_info of component owning */
  312.     /* i'th block in an MCU */
  313.  
  314.     /* these fields are private data for the entropy encoder */
  315.     JCOEF last_dc_val[MAX_COMPS_IN_SCAN]; /* last DC coef for each comp */
  316.     JCOEF last_dc_diff[MAX_COMPS_IN_SCAN]; /* last DC diff for each comp */
  317.     UINT16 restarts_to_go;    /* MCUs left in this restart interval */
  318.     short next_restart_num;    /* # of next RSTn marker (0..7) */
  319. };
  320.  
  321. typedef struct Compress_info_struct * compress_info_ptr;
  322.  
  323.  
  324. /* Working data for decompression */
  325.  
  326. struct Decompress_info_struct {
  327. /*
  328.  * These fields shall be established by the user interface before
  329.  * calling jpeg_decompress.
  330.  * Most parameters can be set to reasonable defaults by j_d_defaults.
  331.  * Note that the UI must supply the storage for the main methods struct,
  332.  * though it sets only a few of the methods there.
  333.  */
  334.     decompress_methods_ptr methods; /* Points to list of methods to use */
  335.  
  336.     external_methods_ptr emethods; /* Points to list of methods to use */
  337.  
  338.     JFILEREF input_file;    /* tells input routines where to read JPEG */
  339.     IFILEREF output_file;    /* tells output routines where to write image */
  340.  
  341.     /* these can be set at d_ui_method_selection time: */
  342.  
  343.     COLOR_SPACE out_color_space; /* colorspace of output */
  344.  
  345.     double output_gamma;    /* image gamma wanted in output */
  346.  
  347.     boolean quantize_colors; /* T if output is a colormapped format */
  348.     /* the following are ignored if not quantize_colors: */
  349.     boolean two_pass_quantize;    /* use two-pass color quantization? */
  350.     boolean use_dithering;        /* want color dithering? */
  351.     int desired_number_of_colors;    /* max number of colors to use */
  352.  
  353.     boolean do_block_smoothing; /* T = apply cross-block smoothing */
  354.     boolean do_pixel_smoothing; /* T = apply post-upsampling smoothing */
  355.  
  356. /*
  357.  * These fields are used for efficient buffering of data between read_jpeg_data
  358.  * and the entropy decoding object.  By using a shared buffer, we avoid copying
  359.  * data and eliminate the need for an "unget" operation at the end of a scan.
  360.  * The actual source of the data is known only to read_jpeg_data; see the
  361.  * JGETC macro, below.
  362.  * Note: the user interface is expected to allocate the input_buffer and
  363.  * initialize bytes_in_buffer to 0.  Also, for JFIF/raw-JPEG input, the UI
  364.  * actually supplies the read_jpeg_data method.  This is all handled by
  365.  * j_d_defaults in a typical implementation.
  366.  */
  367.     char * input_buffer;    /* start of buffer (private to input code) */
  368.     char * next_input_byte;    /* => next byte to read from buffer */
  369.     int bytes_in_buffer;    /* # of bytes remaining in buffer */
  370.  
  371. /*
  372.  * These fields are set by read_file_header or read_scan_header
  373.  */
  374.     long image_width;    /* overall image width */
  375.     long image_height;    /* overall image height */
  376.  
  377.     short data_precision;    /* bits of precision in image data */
  378.  
  379.     COLOR_SPACE jpeg_color_space; /* colorspace of JPEG file */
  380.  
  381.         /* These three values are not used by the JPEG code, merely copied */
  382.     /* from the JFIF APP0 marker (if any). */
  383.     UINT8 density_unit;    /* JFIF code for pixel size units */
  384.     UINT16 X_density;    /* Horizontal pixel density */
  385.     UINT16 Y_density;    /* Vertical pixel density */
  386.  
  387.     short num_components;    /* # of color components in JPEG image */
  388.     jpeg_component_info * comp_info;
  389.     /* comp_info[i] describes component that appears i'th in SOF */
  390.  
  391.     QUANT_TBL_PTR quant_tbl_ptrs[NUM_QUANT_TBLS];
  392.     /* ptrs to coefficient quantization tables, or NULL if not defined */
  393.  
  394.     HUFF_TBL * dc_huff_tbl_ptrs[NUM_HUFF_TBLS];
  395.     HUFF_TBL * ac_huff_tbl_ptrs[NUM_HUFF_TBLS];
  396.     /* ptrs to Huffman coding tables, or NULL if not defined */
  397.  
  398.     UINT8 arith_dc_L[NUM_ARITH_TBLS]; /* L values for DC arith-coding tables */
  399.     UINT8 arith_dc_U[NUM_ARITH_TBLS]; /* U values for DC arith-coding tables */
  400.     UINT8 arith_ac_K[NUM_ARITH_TBLS]; /* Kx values for AC arith-coding tables */
  401.  
  402.     boolean arith_code;    /* TRUE=arithmetic coding, FALSE=Huffman */
  403.     boolean CCIR601_sampling; /* TRUE=first samples are cosited */
  404.  
  405.     UINT16 restart_interval;/* MCUs per restart interval, or 0 for no restart */
  406.  
  407. /*
  408.  * These fields are computed during jpeg_decompress startup
  409.  */
  410.     short max_h_samp_factor; /* largest h_samp_factor */
  411.     short max_v_samp_factor; /* largest v_samp_factor */
  412.  
  413.     short color_out_comps;    /* # of color components output by color_convert */
  414.                 /* (need not match num_components) */
  415.     short final_out_comps;    /* # of color components sent to put_pixel_rows */
  416.     /* (1 when quantizing colors, else same as color_out_comps) */
  417.  
  418.     JSAMPLE * sample_range_limit; /* table for fast range-limiting */
  419.  
  420. /*
  421.  * When quantizing colors, the color quantizer leaves a pointer to the output
  422.  * colormap in these fields.  The colormap is valid from the time put_color_map
  423.  * is called (must be before any put_pixel_rows calls) until shutdown (more
  424.  * specifically, until free_all is called to release memory).
  425.  */
  426.     int actual_number_of_colors; /* actual number of entries */
  427.     JSAMPARRAY colormap;    /* NULL if not valid */
  428.     /* map has color_out_comps rows * actual_number_of_colors columns */
  429.  
  430. /*
  431.  * These fields may be useful for progress monitoring
  432.  */
  433.  
  434.     int total_passes;    /* number of passes expected */
  435.     int completed_passes;    /* number of passes completed so far */
  436.  
  437. /*
  438.  * These fields are valid during any one scan
  439.  */
  440.     short comps_in_scan;    /* # of JPEG components input this time */
  441.     jpeg_component_info * cur_comp_info[MAX_COMPS_IN_SCAN];
  442.     /* *cur_comp_info[i] describes component that appears i'th in SOS */
  443.  
  444.     long MCUs_per_row;    /* # of MCUs across the image */
  445.     long MCU_rows_in_scan;    /* # of MCU rows in the image */
  446.  
  447.     short blocks_in_MCU;    /* # of DCT blocks per MCU */
  448.     short MCU_membership[MAX_BLOCKS_IN_MCU];
  449.     /* MCU_membership[i] is index in cur_comp_info of component owning */
  450.     /* i'th block in an MCU */
  451.  
  452.     /* these fields are private data for the entropy encoder */
  453.     JCOEF last_dc_val[MAX_COMPS_IN_SCAN]; /* last DC coef for each comp */
  454.     JCOEF last_dc_diff[MAX_COMPS_IN_SCAN]; /* last DC diff for each comp */
  455.     UINT16 restarts_to_go;    /* MCUs left in this restart interval */
  456.     short next_restart_num;    /* # of next RSTn marker (0..7) */
  457. };
  458.  
  459. typedef struct Decompress_info_struct * decompress_info_ptr;
  460.  
  461.  
  462. /* Macros for reading data from the decompression input buffer */
  463.  
  464. #ifdef CHAR_IS_UNSIGNED
  465. #define JGETC(cinfo)    ( --(cinfo)->bytes_in_buffer < 0 ? \
  466.              (*(cinfo)->methods->read_jpeg_data) (cinfo) : \
  467.              (int) (*(cinfo)->next_input_byte++) )
  468. #else
  469. #define JGETC(cinfo)    ( --(cinfo)->bytes_in_buffer < 0 ? \
  470.              (*(cinfo)->methods->read_jpeg_data) (cinfo) : \
  471.              (int) (*(cinfo)->next_input_byte++) & 0xFF )
  472. #endif
  473.  
  474. #define JUNGETC(ch,cinfo)  ((cinfo)->bytes_in_buffer++, \
  475.                 *(--((cinfo)->next_input_byte)) = (char) (ch))
  476.  
  477. #define MIN_UNGET    4    /* may always do at least 4 JUNGETCs */
  478.  
  479.  
  480. /* A virtual image has a control block whose contents are private to the
  481.  * memory manager module (and may differ between managers).  The rest of the
  482.  * code only refers to virtual images by these pointer types, and never
  483.  * dereferences the pointer.
  484.  */
  485.  
  486. typedef struct big_sarray_control * big_sarray_ptr;
  487. typedef struct big_barray_control * big_barray_ptr;
  488.  
  489. /* Although a real ANSI C compiler can deal perfectly well with pointers to
  490.  * unspecified structures (see "incomplete types" in the spec), a few pre-ANSI
  491.  * and pseudo-ANSI compilers get confused.  To keep one of these bozos happy,
  492.  * add -DINCOMPLETE_TYPES_BROKEN to CFLAGS in your Makefile.  Then we will
  493.  * pseudo-define the structs as containing a single "dummy" field.
  494.  * The memory managers #define AM_MEMORY_MANAGER before including this file,
  495.  * so that they can make their own definitions of the structs.
  496.  */
  497.  
  498. #ifdef INCOMPLETE_TYPES_BROKEN
  499. #ifndef AM_MEMORY_MANAGER
  500. struct big_sarray_control { long dummy; };
  501. struct big_barray_control { long dummy; };
  502. #endif
  503. #endif
  504.  
  505.  
  506. /* Method types that need typedefs */
  507.  
  508. typedef METHOD(void, MCU_output_method_ptr, (compress_info_ptr cinfo,
  509.                          JBLOCK *MCU_data));
  510. typedef METHOD(void, MCU_output_caller_ptr, (compress_info_ptr cinfo,
  511.                          MCU_output_method_ptr output_method));
  512. typedef METHOD(void, downsample_ptr, (compress_info_ptr cinfo,
  513.                       int which_component,
  514.                       long input_cols, int input_rows,
  515.                       long output_cols, int output_rows,
  516.                       JSAMPARRAY above,
  517.                       JSAMPARRAY input_data,
  518.                       JSAMPARRAY below,
  519.                       JSAMPARRAY output_data));
  520. typedef METHOD(void, upsample_ptr, (decompress_info_ptr cinfo,
  521.                     int which_component,
  522.                     long input_cols, int input_rows,
  523.                     long output_cols, int output_rows,
  524.                     JSAMPARRAY above,
  525.                     JSAMPARRAY input_data,
  526.                     JSAMPARRAY below,
  527.                     JSAMPARRAY output_data));
  528. typedef METHOD(void, quantize_method_ptr, (decompress_info_ptr cinfo,
  529.                        int num_rows,
  530.                        JSAMPIMAGE input_data,
  531.                        JSAMPARRAY output_workspace));
  532. typedef METHOD(void, quantize_caller_ptr, (decompress_info_ptr cinfo,
  533.                        quantize_method_ptr quantize_method));
  534.  
  535.  
  536. /* These structs contain function pointers for the various JPEG methods. */
  537.  
  538. /* Routines to be provided by the surrounding application, rather than the
  539.  * portable JPEG code proper.  These are the same for compression and
  540.  * decompression.
  541.  */
  542.  
  543. struct External_methods_struct {
  544.     /* User interface: error exit and trace message routines */
  545.     /* NOTE: the string msgtext parameters will eventually be replaced
  546.      * by an enumerated-type code so that non-English error messages
  547.      * can be substituted easily.  This will not be done until all the
  548.      * code is in place, so that we know what messages are needed.
  549.      */
  550.     METHOD(void, error_exit, (const char *msgtext));
  551.     METHOD(void, trace_message, (const char *msgtext));
  552.  
  553.     /* Working data for error/trace facility */
  554.     /* See macros below for the usage of these variables */
  555.     int trace_level;    /* level of detail of tracing messages */
  556.     /* Use level 0 for important warning messages (nonfatal errors) */
  557.     /* Use levels 1, 2, 3 for successively more detailed trace options */
  558.  
  559.     /* For recoverable corrupt-data errors, we emit a warning message and
  560.      * keep going.  A surrounding application can check for bad data by
  561.      * seeing if num_warnings is nonzero at the end of processing.
  562.      */
  563.     long num_warnings;    /* number of corrupt-data warnings */
  564.     int first_warning_level; /* trace level for first warning */
  565.     int more_warning_level;    /* trace level for subsequent warnings */
  566.  
  567.     int message_parm[8];    /* store numeric parms for messages here */
  568.  
  569.     /* Memory management */
  570.     /* NB: alloc routines never return NULL. They exit to */
  571.     /* error_exit if not successful. */
  572.     METHOD(void *, alloc_small, (size_t sizeofobject));
  573.     METHOD(void, free_small, (void *ptr));
  574.     METHOD(void FAR *, alloc_medium, (size_t sizeofobject));
  575.     METHOD(void, free_medium, (void FAR *ptr));
  576.     METHOD(JSAMPARRAY, alloc_small_sarray, (long samplesperrow,
  577.                         long numrows));
  578.     METHOD(void, free_small_sarray, (JSAMPARRAY ptr));
  579.     METHOD(JBLOCKARRAY, alloc_small_barray, (long blocksperrow,
  580.                          long numrows));
  581.     METHOD(void, free_small_barray, (JBLOCKARRAY ptr));
  582.     METHOD(big_sarray_ptr, request_big_sarray, (long samplesperrow,
  583.                             long numrows,
  584.                             long unitheight));
  585.     METHOD(big_barray_ptr, request_big_barray, (long blocksperrow,
  586.                             long numrows,
  587.                             long unitheight));
  588.     METHOD(void, alloc_big_arrays, (long extra_small_samples,
  589.                     long extra_small_blocks,
  590.                     long extra_medium_space));
  591.     METHOD(JSAMPARRAY, access_big_sarray, (big_sarray_ptr ptr,
  592.                            long start_row,
  593.                            boolean writable));
  594.     METHOD(JBLOCKARRAY, access_big_barray, (big_barray_ptr ptr,
  595.                         long start_row,
  596.                         boolean writable));
  597.     METHOD(void, free_big_sarray, (big_sarray_ptr ptr));
  598.     METHOD(void, free_big_barray, (big_barray_ptr ptr));
  599.     METHOD(void, free_all, (void));
  600.  
  601.     long max_memory_to_use;    /* maximum amount of memory to use */
  602. };
  603.  
  604. /* Macros to simplify using the error and trace message stuff */
  605. /* The first parameter is generally cinfo->emethods */
  606.  
  607. /* Fatal errors (print message and exit) */
  608. #define ERREXIT(emeth,msg)        ((*(emeth)->error_exit) (msg))
  609. #define ERREXIT1(emeth,msg,p1)        ((emeth)->message_parm[0] = (p1), \
  610.                      (*(emeth)->error_exit) (msg))
  611. #define ERREXIT2(emeth,msg,p1,p2)    ((emeth)->message_parm[0] = (p1), \
  612.                      (emeth)->message_parm[1] = (p2), \
  613.                      (*(emeth)->error_exit) (msg))
  614. #define ERREXIT3(emeth,msg,p1,p2,p3)    ((emeth)->message_parm[0] = (p1), \
  615.                      (emeth)->message_parm[1] = (p2), \
  616.                      (emeth)->message_parm[2] = (p3), \
  617.                      (*(emeth)->error_exit) (msg))
  618. #define ERREXIT4(emeth,msg,p1,p2,p3,p4) ((emeth)->message_parm[0] = (p1), \
  619.                      (emeth)->message_parm[1] = (p2), \
  620.                      (emeth)->message_parm[2] = (p3), \
  621.                      (emeth)->message_parm[3] = (p4), \
  622.                      (*(emeth)->error_exit) (msg))
  623.  
  624. #define MAKESTMT(stuff)        do { stuff } while (0)
  625.  
  626. /* Nonfatal errors (we'll keep going, but the data is probably corrupt) */
  627. /* Note that warning count is incremented as a side-effect! */
  628. #define WARNMS(emeth,msg)    \
  629.   MAKESTMT( if ((emeth)->trace_level >= ((emeth)->num_warnings++ ? \
  630.         (emeth)->more_warning_level : (emeth)->first_warning_level)){ \
  631.         (*(emeth)->trace_message) (msg); } )
  632. #define WARNMS1(emeth,msg,p1)    \
  633.   MAKESTMT( if ((emeth)->trace_level >= ((emeth)->num_warnings++ ? \
  634.         (emeth)->more_warning_level : (emeth)->first_warning_level)){ \
  635.         (emeth)->message_parm[0] = (p1); \
  636.         (*(emeth)->trace_message) (msg); } )
  637. #define WARNMS2(emeth,msg,p1,p2)    \
  638.   MAKESTMT( if ((emeth)->trace_level >= ((emeth)->num_warnings++ ? \
  639.         (emeth)->more_warning_level : (emeth)->first_warning_level)){ \
  640.         (emeth)->message_parm[0] = (p1); \
  641.         (emeth)->message_parm[1] = (p2); \
  642.         (*(emeth)->trace_message) (msg); } )
  643.  
  644. /* Informational/debugging messages */
  645. #define TRACEMS(emeth,lvl,msg)    \
  646.   MAKESTMT( if ((emeth)->trace_level >= (lvl)) { \
  647.         (*(emeth)->trace_message) (msg); } )
  648. #define TRACEMS1(emeth,lvl,msg,p1)    \
  649.   MAKESTMT( if ((emeth)->trace_level >= (lvl)) { \
  650.         (emeth)->message_parm[0] = (p1); \
  651.         (*(emeth)->trace_message) (msg); } )
  652. #define TRACEMS2(emeth,lvl,msg,p1,p2)    \
  653.   MAKESTMT( if ((emeth)->trace_level >= (lvl)) { \
  654.         (emeth)->message_parm[0] = (p1); \
  655.         (emeth)->message_parm[1] = (p2); \
  656.         (*(emeth)->trace_message) (msg); } )
  657. #define TRACEMS3(emeth,lvl,msg,p1,p2,p3)    \
  658.   MAKESTMT( if ((emeth)->trace_level >= (lvl)) { \
  659.         int * _mp = (emeth)->message_parm; \
  660.         *_mp++ = (p1); *_mp++ = (p2); *_mp = (p3); \
  661.         (*(emeth)->trace_message) (msg); } )
  662. #define TRACEMS4(emeth,lvl,msg,p1,p2,p3,p4)    \
  663.   MAKESTMT( if ((emeth)->trace_level >= (lvl)) { \
  664.         int * _mp = (emeth)->message_parm; \
  665.         *_mp++ = (p1); *_mp++ = (p2); *_mp++ = (p3); *_mp = (p4); \
  666.         (*(emeth)->trace_message) (msg); } )
  667. #define TRACEMS8(emeth,lvl,msg,p1,p2,p3,p4,p5,p6,p7,p8)    \
  668.   MAKESTMT( if ((emeth)->trace_level >= (lvl)) { \
  669.         int * _mp = (emeth)->message_parm; \
  670.         *_mp++ = (p1); *_mp++ = (p2); *_mp++ = (p3); *_mp++ = (p4); \
  671.         *_mp++ = (p5); *_mp++ = (p6); *_mp++ = (p7); *_mp = (p8); \
  672.         (*(emeth)->trace_message) (msg); } )
  673.  
  674.  
  675. /* Methods used during JPEG compression. */
  676.  
  677. struct Compress_methods_struct {
  678.     /* Hook for user interface to get control after input_init */
  679.     METHOD(void, c_ui_method_selection, (compress_info_ptr cinfo));
  680.     /* Hook for user interface to do progress monitoring */
  681.     METHOD(void, progress_monitor, (compress_info_ptr cinfo,
  682.                     long loopcounter, long looplimit));
  683.     /* Input image reading & conversion to standard form */
  684.     METHOD(void, input_init, (compress_info_ptr cinfo));
  685.     METHOD(void, get_input_row, (compress_info_ptr cinfo,
  686.                      JSAMPARRAY pixel_row));
  687.     METHOD(void, input_term, (compress_info_ptr cinfo));
  688.     /* Color space and gamma conversion */
  689.     METHOD(void, colorin_init, (compress_info_ptr cinfo));
  690.     METHOD(void, get_sample_rows, (compress_info_ptr cinfo,
  691.                        int rows_to_read,
  692.                        JSAMPIMAGE image_data));
  693.     METHOD(void, colorin_term, (compress_info_ptr cinfo));
  694.     /* Expand picture data at edges */
  695.     METHOD(void, edge_expand, (compress_info_ptr cinfo,
  696.                    long input_cols, int input_rows,
  697.                    long output_cols, int output_rows,
  698.                    JSAMPIMAGE image_data));
  699.     /* Downsample pixel values of a single component */
  700.     /* There can be a different downsample method for each component */
  701.     METHOD(void, downsample_init, (compress_info_ptr cinfo));
  702.     downsample_ptr downsample[MAX_COMPS_IN_SCAN];
  703.     METHOD(void, downsample_term, (compress_info_ptr cinfo));
  704.     /* Extract samples in MCU order, process & hand off to output_method */
  705.     /* The input is always exactly N MCU rows worth of data */
  706.     METHOD(void, extract_init, (compress_info_ptr cinfo));
  707.     METHOD(void, extract_MCUs, (compress_info_ptr cinfo,
  708.                     JSAMPIMAGE image_data,
  709.                     int num_mcu_rows,
  710.                     MCU_output_method_ptr output_method));
  711.     METHOD(void, extract_term, (compress_info_ptr cinfo));
  712.     /* Entropy encoding parameter optimization */
  713.     METHOD(void, entropy_optimize, (compress_info_ptr cinfo,
  714.                     MCU_output_caller_ptr source_method));
  715.     /* Entropy encoding */
  716.     METHOD(void, entropy_encode_init, (compress_info_ptr cinfo));
  717.     METHOD(void, entropy_encode, (compress_info_ptr cinfo,
  718.                       JBLOCK *MCU_data));
  719.     METHOD(void, entropy_encode_term, (compress_info_ptr cinfo));
  720.     /* JPEG file header construction */
  721.     METHOD(void, write_file_header, (compress_info_ptr cinfo));
  722.     METHOD(void, write_scan_header, (compress_info_ptr cinfo));
  723.     METHOD(void, write_jpeg_data, (compress_info_ptr cinfo,
  724.                        char *dataptr,
  725.                        int datacount));
  726.     METHOD(void, write_scan_trailer, (compress_info_ptr cinfo));
  727.     METHOD(void, write_file_trailer, (compress_info_ptr cinfo));
  728.     /* Pipeline control */
  729.     METHOD(void, c_pipeline_controller, (compress_info_ptr cinfo));
  730.     METHOD(void, entropy_output, (compress_info_ptr cinfo,
  731.                       char *dataptr,
  732.                       int datacount));
  733.     /* Overall control */
  734.     METHOD(void, c_per_scan_method_selection, (compress_info_ptr cinfo));
  735. };
  736.  
  737. /* Methods used during JPEG decompression. */
  738.  
  739. struct Decompress_methods_struct {
  740.     /* Hook for user interface to get control after reading file header */
  741.     METHOD(void, d_ui_method_selection, (decompress_info_ptr cinfo));
  742.     /* Hook for user interface to do progress monitoring */
  743.     METHOD(void, progress_monitor, (decompress_info_ptr cinfo,
  744.                     long loopcounter, long looplimit));
  745.     /* JPEG file scanning */
  746.     METHOD(void, read_file_header, (decompress_info_ptr cinfo));
  747.     METHOD(boolean, read_scan_header, (decompress_info_ptr cinfo));
  748.     METHOD(int, read_jpeg_data, (decompress_info_ptr cinfo));
  749.     METHOD(void, resync_to_restart, (decompress_info_ptr cinfo,
  750.                      int marker));
  751.     METHOD(void, read_scan_trailer, (decompress_info_ptr cinfo));
  752.     METHOD(void, read_file_trailer, (decompress_info_ptr cinfo));
  753.     /* Entropy decoding */
  754.     METHOD(void, entropy_decode_init, (decompress_info_ptr cinfo));
  755.     METHOD(void, entropy_decode, (decompress_info_ptr cinfo,
  756.                       JBLOCKROW *MCU_data));
  757.     METHOD(void, entropy_decode_term, (decompress_info_ptr cinfo));
  758.     /* MCU disassembly: fetch MCUs from entropy_decode, build coef array */
  759.     /* The reverse_DCT step is in the same module for symmetry reasons */
  760.     METHOD(void, disassemble_init, (decompress_info_ptr cinfo));
  761.     METHOD(void, disassemble_MCU, (decompress_info_ptr cinfo,
  762.                        JBLOCKIMAGE image_data));
  763.     METHOD(void, reverse_DCT, (decompress_info_ptr cinfo,
  764.                    JBLOCKIMAGE coeff_data,
  765.                    JSAMPIMAGE output_data, int start_row));
  766.     METHOD(void, disassemble_term, (decompress_info_ptr cinfo));
  767.     /* Cross-block smoothing */
  768.     METHOD(void, smooth_coefficients, (decompress_info_ptr cinfo,
  769.                        jpeg_component_info *compptr,
  770.                        JBLOCKROW above,
  771.                        JBLOCKROW currow,
  772.                        JBLOCKROW below,
  773.                        JBLOCKROW output));
  774.     /* Upsample pixel values of a single component */
  775.     /* There can be a different upsample method for each component */
  776.     METHOD(void, upsample_init, (decompress_info_ptr cinfo));
  777.     upsample_ptr upsample[MAX_COMPS_IN_SCAN];
  778.     METHOD(void, upsample_term, (decompress_info_ptr cinfo));
  779.     /* Color space and gamma conversion */
  780.     METHOD(void, colorout_init, (decompress_info_ptr cinfo));
  781.     METHOD(void, color_convert, (decompress_info_ptr cinfo,
  782.                      int num_rows, long num_cols,
  783.                      JSAMPIMAGE input_data,
  784.                      JSAMPIMAGE output_data));
  785.     METHOD(void, colorout_term, (decompress_info_ptr cinfo));
  786.     /* Color quantization */
  787.     METHOD(void, color_quant_init, (decompress_info_ptr cinfo));
  788.     METHOD(void, color_quantize, (decompress_info_ptr cinfo,
  789.                       int num_rows,
  790.                       JSAMPIMAGE input_data,
  791.                       JSAMPARRAY output_data));
  792.     METHOD(void, color_quant_prescan, (decompress_info_ptr cinfo,
  793.                        int num_rows,
  794.                        JSAMPIMAGE image_data,
  795.                        JSAMPARRAY workspace));
  796.     METHOD(void, color_quant_doit, (decompress_info_ptr cinfo,
  797.                     quantize_caller_ptr source_method));
  798.     METHOD(void, color_quant_term, (decompress_info_ptr cinfo));
  799.     /* Output image writing */
  800.     METHOD(void, output_init, (decompress_info_ptr cinfo));
  801.     METHOD(void, put_color_map, (decompress_info_ptr cinfo,
  802.                      int num_colors, JSAMPARRAY colormap));
  803.     METHOD(void, put_pixel_rows, (decompress_info_ptr cinfo,
  804.                       int num_rows,
  805.                       JSAMPIMAGE pixel_data));
  806.     METHOD(void, output_term, (decompress_info_ptr cinfo));
  807.     /* Pipeline control */
  808.     METHOD(void, d_pipeline_controller, (decompress_info_ptr cinfo));
  809.     /* Overall control */
  810.     METHOD(void, d_per_scan_method_selection, (decompress_info_ptr cinfo));
  811. };
  812.  
  813.  
  814. /* External declarations for routines that aren't called via method ptrs. */
  815. /* Note: use "j" as first char of names to minimize namespace pollution. */
  816. /* The PP macro hides prototype parameters from compilers that can't cope. */
  817.  
  818. #ifdef PROTO
  819. #define PP(arglist)    arglist
  820. #else
  821. #define PP(arglist)    ()
  822. #endif
  823.  
  824.  
  825. /* main entry for compression */
  826. EXTERN void jpeg_compress PP((compress_info_ptr cinfo));
  827.  
  828. /* default parameter setup for compression */
  829. EXTERN void j_c_defaults PP((compress_info_ptr cinfo, int quality,
  830.                  boolean force_baseline));
  831. EXTERN void j_monochrome_default PP((compress_info_ptr cinfo));
  832. EXTERN void j_set_quality PP((compress_info_ptr cinfo, int quality,
  833.                   boolean force_baseline));
  834. /* advanced compression parameter setup aids */
  835. EXTERN void j_add_quant_table PP((compress_info_ptr cinfo, int which_tbl,
  836.                   const QUANT_VAL *basic_table,
  837.                   int scale_factor, boolean force_baseline));
  838. EXTERN int j_quality_scaling PP((int quality));
  839.  
  840. /* main entry for decompression */
  841. EXTERN void jpeg_decompress PP((decompress_info_ptr cinfo));
  842.  
  843. /* default parameter setup for decompression */
  844. EXTERN void j_d_defaults PP((decompress_info_ptr cinfo,
  845.                  boolean standard_buffering));
  846.  
  847. /* forward DCT */
  848. EXTERN void j_fwd_dct PP((DCTBLOCK data));
  849. /* inverse DCT */
  850. EXTERN void j_rev_dct PP((DCTBLOCK data));
  851.  
  852. /* utility routines in jutils.c */
  853. EXTERN long jround_up PP((long a, long b));
  854. EXTERN void jcopy_sample_rows PP((JSAMPARRAY input_array, int source_row,
  855.                   JSAMPARRAY output_array, int dest_row,
  856.                   int num_rows, long num_cols));
  857. EXTERN void jcopy_block_row PP((JBLOCKROW input_row, JBLOCKROW output_row,
  858.                 long num_blocks));
  859. EXTERN void jzero_far PP((void FAR * target, size_t bytestozero));
  860.  
  861. /* method selection routines for compression modules */
  862. EXTERN void jselcpipeline PP((compress_info_ptr cinfo)); /* jcpipe.c */
  863. EXTERN void jselchuffman PP((compress_info_ptr cinfo)); /* jchuff.c */
  864. EXTERN void jselcarithmetic PP((compress_info_ptr cinfo)); /* jcarith.c */
  865. EXTERN void jselexpand PP((compress_info_ptr cinfo)); /* jcexpand.c */
  866. EXTERN void jseldownsample PP((compress_info_ptr cinfo)); /* jcsample.c */
  867. EXTERN void jselcmcu PP((compress_info_ptr cinfo)); /* jcmcu.c */
  868. EXTERN void jselccolor PP((compress_info_ptr cinfo));    /* jccolor.c */
  869. /* The user interface should call one of these to select input format: */
  870. EXTERN void jselrgif PP((compress_info_ptr cinfo)); /* jrdgif.c */
  871. EXTERN void jselrppm PP((compress_info_ptr cinfo)); /* jrdppm.c */
  872. EXTERN void jselrrle PP((compress_info_ptr cinfo)); /* jrdrle.c */
  873. EXTERN void jselrtarga PP((compress_info_ptr cinfo)); /* jrdtarga.c */
  874. /* and one of these to select output header format: */
  875. EXTERN void jselwjfif PP((compress_info_ptr cinfo)); /* jwrjfif.c */
  876.  
  877. /* method selection routines for decompression modules */
  878. EXTERN void jseldpipeline PP((decompress_info_ptr cinfo)); /* jdpipe.c */
  879. EXTERN void jseldhuffman PP((decompress_info_ptr cinfo)); /* jdhuff.c */
  880. EXTERN void jseldarithmetic PP((decompress_info_ptr cinfo)); /* jdarith.c */
  881. EXTERN void jseldmcu PP((decompress_info_ptr cinfo)); /* jdmcu.c */
  882. EXTERN void jselbsmooth PP((decompress_info_ptr cinfo)); /* jbsmooth.c */
  883. EXTERN void jselupsample PP((decompress_info_ptr cinfo)); /* jdsample.c */
  884. EXTERN void jseldcolor PP((decompress_info_ptr cinfo));    /* jdcolor.c */
  885. EXTERN void jsel1quantize PP((decompress_info_ptr cinfo)); /* jquant1.c */
  886. EXTERN void jsel2quantize PP((decompress_info_ptr cinfo)); /* jquant2.c */
  887. /* The user interface should call one of these to select input format: */
  888. EXTERN void jselrjfif PP((decompress_info_ptr cinfo)); /* jrdjfif.c */
  889. /* and one of these to select output image format: */
  890. EXTERN void jselwgif PP((decompress_info_ptr cinfo)); /* jwrgif.c */
  891. EXTERN void jselwppm PP((decompress_info_ptr cinfo)); /* jwrppm.c */
  892. EXTERN void jselwrle PP((decompress_info_ptr cinfo)); /* jwrrle.c */
  893. EXTERN void jselwtarga PP((decompress_info_ptr cinfo)); /* jwrtarga.c */
  894.  
  895. /* method selection routines for system-dependent modules */
  896. EXTERN void jselerror PP((external_methods_ptr emethods)); /* jerror.c */
  897. EXTERN void jselmemmgr PP((external_methods_ptr emethods)); /* jmemmgr.c */
  898.  
  899.  
  900. /* We assume that right shift corresponds to signed division by 2 with
  901.  * rounding towards minus infinity.  This is correct for typical "arithmetic
  902.  * shift" instructions that shift in copies of the sign bit.  But some
  903.  * C compilers implement >> with an unsigned shift.  For these machines you
  904.  * must define RIGHT_SHIFT_IS_UNSIGNED.
  905.  * RIGHT_SHIFT provides a proper signed right shift of an INT32 quantity.
  906.  * It is only applied with constant shift counts.  SHIFT_TEMPS must be
  907.  * included in the variables of any routine using RIGHT_SHIFT.
  908.  */
  909.  
  910. #ifdef RIGHT_SHIFT_IS_UNSIGNED
  911. #define SHIFT_TEMPS    INT32 shift_temp;
  912. #define RIGHT_SHIFT(x,shft)  \
  913.     ((shift_temp = (x)) < 0 ? \
  914.      (shift_temp >> (shft)) | ((~((INT32) 0)) << (32-(shft))) : \
  915.      (shift_temp >> (shft)))
  916. #else
  917. #define SHIFT_TEMPS
  918. #define RIGHT_SHIFT(x,shft)    ((x) >> (shft))
  919. #endif
  920.  
  921.  
  922. /* Miscellaneous useful macros */
  923.  
  924. #undef MAX
  925. #define MAX(a,b)    ((a) > (b) ? (a) : (b))
  926. #undef MIN
  927. #define MIN(a,b)    ((a) < (b) ? (a) : (b))
  928.  
  929.  
  930. #define RST0    0xD0        /* RST0 marker code */
  931.